package cn.kpi.service.payassessment.impl;

import cn.kpi.dao.attendance.AttFjGdDao;
import cn.kpi.dao.common.OrgSummaryDao;
import cn.kpi.dao.payassessment.PayassessmentDao;
import cn.kpi.dao.payassessment.TUserPayDAO;
import cn.kpi.entity.attendance.AttTbBatchEntity;
import cn.kpi.entity.payassessment.*;
import cn.kpi.service.payassessment.IPayAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayAssessmentServiceImpl implements IPayAssessmentService {

	@Autowired
	private PayassessmentDao payassessmentDao;

	@Autowired
	private TUserPayDAO tUserPayDAO;

	@Autowired
	private OrgSummaryDao orgSummaryDao;

	@Autowired
	private AttFjGdDao attFjGdDao;


	@Override
	public List<AssessRulePay> getAssessRuleList() {
		try {
				return payassessmentDao.getAssessRuleList();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public AssessRulePay getAssessRuleByCode(String mCode) {
		try {
			return payassessmentDao.getAssessbyCode(mCode);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int UpdateAssessRuleBatch(List<AssessRulePay> list) throws Exception {
	 	try{
			int result = payassessmentDao.UpdateAssessRuleBatch(list);
			return result;
		}
			catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<TransportationPayVoEntity> getTranPayVoEntity(String where) {
		try{
			return payassessmentDao.GetTranPayListByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int getTranPayVoEntityCount(String where) {
		try{
			return payassessmentDao.GetTranPayListCountByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int updateTranPayListByid(List<TransportationPayVoEntity> payVoEntities)  throws Exception{
		try {
			int result =0;
//				result = payassessmentDao.deleteTranPay(where);
				result += payassessmentDao.updateTranPayListByid(payVoEntities);
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int updateTrancePayList(List<TransportationPayVoEntity> payVoEntities,String status) throws Exception {
		try {
			int result =0;
			result = payassessmentDao.updateTranPayList(payVoEntities);
			if(status.equals("3")) {//更新工资表的交通补助
				result = tUserPayDAO.updatePayListTran(payVoEntities);
			}
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	@Transactional
	public int createTrancePayList(AttTbBatchEntity entity, List<TransportationPayVoEntity> payVoEntities) throws Exception {
		try{
			int result=0;
			entity.setOrgsmytype("tran");
			result+=orgSummaryDao.InsertOrgSummary(entity);
			Long orgsummaryid=entity.getOrgsummaryid();
			//System.out.println(orgsummaryid);
			for (TransportationPayVoEntity payVoEntity : payVoEntities) {
				payVoEntity.setOrgsummaryid(orgsummaryid);
			}

			result+=payassessmentDao.insertTranPayList(payVoEntities);

			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	@Transactional
	public int UpdateTranAttStatus(Long orgsummaryid, int status) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateTranPay(orgsummaryid, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int UpdateTranAttStatusBm(String orgsummaryids, int status) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateTranPayBm(orgsummaryids, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatusBm(orgsummaryids, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	@Transactional
	public int UpdateTranAttStatus(Long orgsummaryid, int status, List<TransportationPayVoEntity> payVoEntities) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateTranPay(orgsummaryid, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			result+= tUserPayDAO.updatePayListTran(payVoEntities);//归并到工资表
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public List<WorkPayVoEntity> getWorkPayVoEntity(String where) {
		try{
			return payassessmentDao.GetWorkPayListByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<WorkSeanVoEntity> getWorkSeanPayEntiy(String where,String whereLimit, String month1, String month2, String month3,String month) {
		try{
			return payassessmentDao.GetWorkPaySeasonListByWhere(where,whereLimit,month1, month2, month3,month);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int getWorkPayVoCountEntity(String where) {
		try{
			return payassessmentDao.GetWorkPayListCountByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int createWorkPayList(AttTbBatchEntity entity, List<WorkPayVoEntity> payVoEntities) throws Exception {
		try{
			int result=0;
			entity.setOrgsmytype("work");
			result+=orgSummaryDao.InsertOrgSummary(entity);
			Long orgsummaryid=entity.getOrgsummaryid();
			//System.out.println(orgsummaryid);
			for (WorkPayVoEntity payVoEntity : payVoEntities) {
				payVoEntity.setOrgsummaryid(orgsummaryid);
			}

			result+=payassessmentDao.insertWorkPayList(payVoEntities);

			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int createDeptSubList(AttTbBatchEntity entity, List<DeptSWPayVoEntity> payVoEntities) throws Exception {
		try{
			int result=0;
			entity.setOrgsmytype("deptsub");
			result+=orgSummaryDao.InsertOrgSummary(entity);
			Long orgsummaryid=entity.getOrgsummaryid();
			//System.out.println(orgsummaryid);
			for (DeptSWPayVoEntity payVoEntity : payVoEntities) {
				payVoEntity.setOrgsummaryid(orgsummaryid);
			}

			result+=payassessmentDao.insertDeptSWPayList(payVoEntities);

			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}


	@Override
	public int saveWorkPayList(List<WorkPayVoEntity> payVoEntities, String where) throws Exception {
		try {
			int result =0;
			if(where.contains("year")&&where.contains("month")&&where.contains("userid")) {
				result = payassessmentDao.deleteWorkPay(where);
				result += payassessmentDao.insertWorkPayList(payVoEntities);
			}
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int updateWorkPayList(List<WorkPayVoEntity> payVoEntities) throws Exception {
		try {
			//批量update不报错就是成功
			int result = payassessmentDao.updateWorkPayList(payVoEntities);
			return 1;

		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int updateWorkPayListByid(List<WorkPayVoEntity> payVoEntities) throws Exception {
		try {
			int result =0;
			result += payassessmentDao.updateWorkPayListByid(payVoEntities);
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	@Transactional
	public int UpdateWorkAttStatus(Long orgsummaryid, int status) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateWorkpay(orgsummaryid, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int UpdateWorkAttStatusWhere(String where, int status) throws Exception {
		try{
			int result=0;
//			result+=payassessmentDao.updateWorkpayWhere(where, status);//放弃自己的状态，以季度状态为准
			where += " AND orgsmytype = 'work'";
			result+=attFjGdDao.UpdateAttOrgSummaryStatusWhere(where, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int UpdateDeptSubAttStatus(Long orgsummaryid, int status) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateDeptSWPay(orgsummaryid, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int UpdateDeptSubAttStatusWhere(String where, int status) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateDeptSWPayWhere(where, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatusWhere(where, status);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public int UpdateWorkAttStatus(Long orgsummaryid, int status, List<WorkPayVoEntity> payVoEntities) throws Exception {
		try{
			int result=0;
			result+=payassessmentDao.updateWorkpay(orgsummaryid, status);
			result+=attFjGdDao.UpdateAttOrgSummaryStatus(orgsummaryid, status);
			result+= tUserPayDAO.updatePayListWork(payVoEntities);//归并到工资表
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}

	@Override
	public List<DeptSWPayVoEntity> getDeptPayVoEntity(String where) {
		try{
			return payassessmentDao.GetDeptSWPayListByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int getDeptPayVoEntityCount(String where) {
		try{
			return payassessmentDao.GetDeptSWPayListCountByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

    @Override
    public int saveDeptPayList(List<DeptSWPayVoEntity> payVoEntities, String where) throws Exception {
		try {
			int result =0;
			if(where.contains("year")&&where.contains("month")&&where.contains("userid")) {
				result = payassessmentDao.deleteDeptSWPay(where);
				result += payassessmentDao.insertDeptSWPayList(payVoEntities);
			}
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
			ex.printStackTrace();
			return -1;
		}
    }

    public int saveDeptPay(List<DeptSWPayVoEntity> payVoEntities){
		try {
			return payassessmentDao.insertDeptSWPayList(payVoEntities);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
    @Transactional
	public int updateDeptPayList(List<DeptSWPayVoEntity> payVoEntities, String status, String Where) throws Exception {
		try {
			int result =0;
			result = payassessmentDao.updateDeptSWPayWhere(Where ,Integer.valueOf(status));
//			if(status.equals("3")) {//更新工资表的工作津贴（年）
//				result += tUserPayDAO.updatePayListDeptSW(payVoEntities);
//			}
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
			return -1;
		}
	}

    @Override
    public int updateDeptPayList(List<DeptSWPayVoEntity> payVoEntities) throws Exception {
        try{
            return payassessmentDao.updateDeptSWPayListByid(payVoEntities);
        }
        catch(Exception ex){
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
	public int updateallowPayList(List<AllowancePayVoEntity> allowancePayVoEntities, String yearid , String monthid) throws Exception {
	 	Map<String,AllowanceOrgPayEntity> arps = new HashMap<>();
	 	List<AllowancePayEntity> aps = new ArrayList<>();
		for (AllowancePayVoEntity allowancePayVoEntity : allowancePayVoEntities) {
			AllowancePayEntity ap = new AllowancePayEntity();
			ap.setYearid(yearid);
			ap.setMonthid(monthid);
			ap.setOrgid(allowancePayVoEntity.getOrgid());
			ap.setUserno(allowancePayVoEntity.getUserno());
			ap.setRemark(allowancePayVoEntity.getRemark());
			ap.setRealname(allowancePayVoEntity.getRealname());
			ap.setNowMoney(allowancePayVoEntity.getNowMoney());
			ap.setAllowanceLeave(allowancePayVoEntity.getAllowanceLeave());
			aps.add(ap);
			if(arps.containsKey(allowancePayVoEntity.getOrgid().toString())){
				AllowanceOrgPayEntity arp = arps.get(allowancePayVoEntity.getOrgid());
				arp.setMonthnum(arp.getMonthnum()+1);//人数＋１
				arp.setAllowPay(arp.getAllowPay()+allowancePayVoEntity.getNowMoney());//金额合计
			}else{
				AllowanceOrgPayEntity arp = new AllowanceOrgPayEntity();
				arp.setMonthid(monthid);
				arp.setYearid(yearid);
				arp.setOrgid(allowancePayVoEntity.getOrgid());
				arp.setAllowPay(allowancePayVoEntity.getNowMoney());
				arp.setMaxAllowPay(0l);
				arps.put(allowancePayVoEntity.getOrgid().toString(),arp);
			}
		}
		int ret = payassessmentDao.insertAllPayList(aps);
		ret += payassessmentDao.insertOrgPayList(new ArrayList<AllowanceOrgPayEntity>(arps.values()));
		return ret;
	}

	@Override
	public List<AllowanceOrgPayEntity> getOrgPayListWhere(String where) throws Exception {
		try{
			return payassessmentDao.getOrgPayListWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<AllowancePayEntity> getPayListWhere(String where) throws Exception {
		try{
			return payassessmentDao.getAllPayListWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<TUserPay> getUserPayWhere(String where) throws Exception {
		try{
			return tUserPayDAO.getTuserPayList(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<UserOrgMapEntity> getUserAndOrgid(String where) throws Exception {
		try{
			return tUserPayDAO.getUserAndOrgid(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int getUserPayWhereCount(String where) throws Exception {
		try{
			return tUserPayDAO.getTuserPayListCount(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}

	@Override
	public int updateUserPayList(List<TUserPay> tUserPays, String year, String month) throws Exception {
		try {
			int result =0;
			result=tUserPayDAO.deleteTuserPayByYearAndMonth(year,month);
			result += tUserPayDAO.insertUserPayList(tUserPays);
			if(result<=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
				return -1;
			}
			return result;

		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<outWorkEntity> getOutWork(String where) throws Exception {
		try{
			return tUserPayDAO.getOutWorkPayList(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int saveDeptUserList(DpUserListEntity dpUserListEntities) throws Exception {
		try{
			return payassessmentDao.saveDeptUserList(dpUserListEntities);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}

	@Override
	public int updateDeptUserList(DpUserListEntity dpUserListEntities) throws Exception {
		try{
			return payassessmentDao.updateDeptUserList(dpUserListEntities);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}

	@Override
	public int deleteDeptUserList(Long id) throws Exception {
		try{
			return payassessmentDao.deleteDeptUserList(id);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}

	@Override
	public DpUserListEntity getDeptUser(Long id) throws Exception {
		try{
			return payassessmentDao.getDetpUser(id);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<DpUserListEntity> getDetpUserList(String where) throws Exception {
		try{
			return payassessmentDao.getDetpUserList(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public int getDetpUserCount(String where) throws Exception {
		try{
			return payassessmentDao.getDetpUserCount(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}

}
